//
//  ZNWBPicturesView.swift
//  ZNWeibo
//
//  Created by FunctionMaker on 2017/8/23.
//  Copyright © 2017年 FunctionMaker. All rights reserved.
//

import UIKit

class ZNWBPicturesView: UIView {
    
    // FIXME: - 没有显示的视图将其从父视图移除？
    var viewModel: ZNWBStatusViewModel? {
        didSet {
            // 设置配图视图URL(可能是被转发)
            urls = viewModel?.picURLs
            
            // 根据配图视图大小调整显示内容
            
            // 设置配图视图高度
            heightCos.constant = viewModel?.picturesViewSize.height ?? 0
            
            // 处理宽度
            // 单图
            
            if viewModel?.picURLs?.count ?? 0 == 1 {
                let viewSize = viewModel?.picturesViewSize ?? CGSize()
                
                subviews[0].frame = CGRect(x: 0, y: ZNWBPicturesViewOutterMargin, width: viewSize.width, height: viewSize.height - ZNWBPicturesViewOutterMargin)
            } else {
                // 调整被复用的Cell
                subviews[0].frame = CGRect(x: 0, y: ZNWBPicturesViewOutterMargin, width: ZNWBPictureCellWidth, height: ZNWBPictureCellWidth)
            }
        }
    }
    
    private var urls: [ZNWBStatusPicture]? {
        didSet {
            // 1. 隐藏所有图像
            for view in subviews {
                view.isHidden = true
            }
            
            var index = 0
            
            // 2. 设置图像
            for url in urls ?? [] {
                let iv = subviews[index] as! UIImageView
                
                // 如果配图数为4，则以田字格布局。
                if index == 1 && urls?.count == 4 {
                    index += 1
                }
                
                iv.zn_setImage(urlString: url.thumbnail_pic, placeholderImage: nil)
                iv.isHidden = false
                                
                index += 1
            }
        }
    }
    
    @IBOutlet weak var heightCos: NSLayoutConstraint!
    
    override func awakeFromNib() {
        super.awakeFromNib()
        
        setupUI()
    }
}

extension ZNWBPicturesView {
    
    //1. 为了保证滚动流畅，所有的控件都应当提前准备好
    //2. 根据数据是否显示
    //3. 不要动态创建控件
    fileprivate func setupUI() {
        // 超出边界内容不显示
        clipsToBounds = true
        
        backgroundColor = superview?.backgroundColor
        
        // 循环创建9个UIImageView
        let rowMax = 3
        let rect = CGRect(x: 0,
                          y: ZNWBPicturesViewOutterMargin,
                          width: ZNWBPictureCellWidth,
                          height: ZNWBPictureCellWidth)
        
        for i in 0..<rowMax * rowMax {
            let imageView = UIImageView()
            
            imageView.contentMode = .scaleAspectFill
            
            // 保持纵横比适合视图，图片很可能会跳出view的边界，故需要裁剪超出的部分
            imageView.clipsToBounds = true
            // 行
            let row = CGFloat(i / rowMax)
            // 列
            let col = CGFloat(i % rowMax)
            
            let xOffset = col * (ZNWBPictureCellWidth + ZNWBPicturesViewInnerMargin)
            let yOffset = row * (ZNWBPictureCellWidth + ZNWBPicturesViewInnerMargin)
            // 使用移位来设置frame
            imageView.frame = rect.offsetBy(dx: xOffset, dy: yOffset)
            
            addSubview(imageView)
        }
    }
}
